<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/1/28
 * Time: 下午3:16
 */

/**Example #1 mb_convert_case() 例子*/
$str = "mary had a Little lamb and she loved it so";
$str = mb_convert_case( $str, MB_CASE_UPPER, "UTF-8" ) . PHP_EOL;
echo $str; // 输出 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
$str = mb_convert_case( $str, MB_CASE_TITLE, "UTF-8" ) . PHP_EOL;
echo $str; // 输出 Mary Had A Little Lamb And She Loved It So

/**Example #2 非拉丁 UTF-8 文本的mb_convert_case() 例子*/
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_convert_case( $str, MB_CASE_UPPER, "UTF-8" ) . PHP_EOL;
echo $str; // 输出 ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ
$str = mb_convert_case( $str, MB_CASE_TITLE, "UTF-8" ) . PHP_EOL;
echo $str; // 输出 Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει Υπέρ Νωθρού Κυνόσ

/**
 * mb_strtolower() - 使字符串小写
 * mb_strtoupper() - 使字符串大写
 * strtolower() - 将字符串转化为小写
 * strtoupper() - 将字符串转化为大写
 * ucfirst() - 将字符串的首字母转换为大写
 * ucwords() - 将字符串中每个单词的首字母转换为大写
 */

echo mb_convert_case( 'AAA "aaa"', MB_CASE_TITLE ) . PHP_EOL; //Aaa "aaa"
// but  I want this ===> AAA "Aaa"

function mb_convert_case_utf8_variation( $s ) {
	$arr    = preg_split( "//u", $s, - 1, PREG_SPLIT_NO_EMPTY );
	var_dump($arr);
	$result = "";
	$mode   = false;
	foreach ( $arr as $char ) {
		$res = preg_match(
			       '/\\p{Mn}|\\p{Me}|\\p{Cf}|\\p{Lm}|\\p{Sk}|\\p{Lu}|\\p{Ll}|' .
			       '\\p{Lt}|\\p{Sk}|\\p{Cs}/u', $char ) == 1;
		if ( $mode ) {
			if ( ! $res ) {
				$mode = false;
			}
		} elseif ( $res ) {
			$mode = true;
			$char = mb_convert_case( $char, MB_CASE_TITLE, "UTF-8" );
		}
		$result .= $char;
	}
	
	return $result;
}

echo mb_convert_case_utf8_variation('AAA "aaa"').PHP_EOL;
//AAA "Aaa"

echo mb_convert_case("Hello 中国",MB_CASE_UPPER).PHP_EOL;//HELLO 中国
echo mb_convert_case("Hello 中国",MB_CASE_UPPER,"GBK").PHP_EOL;//HELLO 中国
